using System;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Drawing;
using System.Xml.Serialization;
using System.Windows.Forms;
using BodySoundSimulator.AbsoluteMapData;

namespace BodySoundSimulator.CaseData
{
    public class CaseInformation
    {
        #region Fields
        float version1000 = 10.00f;
        Collection<AuscultationSiteInfo> frontViewAuscultationSites;
        Collection<AuscultationSiteInfo> backViewAuscultationSites;
        string caseID;
        string patientsClaim;
        string diseaseName;
        string soundName;
        SoundMapData soundMap;
        #endregion

        #region Properties
        public float LatestVersion
        {
            get
            {
                return version1000;
            }
        }

        public Collection<AuscultationSiteInfo> FrontViewAuscultationSites
        {
            get { return frontViewAuscultationSites; }
            set { frontViewAuscultationSites = value; }
        }

        public Collection<AuscultationSiteInfo> BackViewAuscultationSites
        {
            get { return backViewAuscultationSites; }
            set { backViewAuscultationSites = value; }
        }

        public string CaseID
        {
            get { return caseID; }
            set { caseID = value; }
        }

        public string PatientsClaim
        {
            get { return patientsClaim; }
            set { patientsClaim = value; }
        }

        public string DiseaseName
        {
            get { return diseaseName; }
            set { diseaseName = value; }
        }

        public string SoundName
        {
            get { return soundName; }
            set { soundName = value; }
        }

        public SoundMapData SoundMap
        {
            get { return soundMap; }
            set { soundMap = value; }
        }
        #endregion

        #region Constructors
        public CaseInformation()
        {
        }
        #endregion

        #region Public methods
        public void InitializeDefaults()
        {
            InitDefaultAuscultationSiteInfo();
            InitDefaultSoundMap();
        }

        public void XmlSerialize(string filePath)
        {
            CaseInformation caseInfo = this;
            if (this == null)
            {
                caseInfo = new CaseInformation();
            }
            StreamWriter streamWriter = null;
            try
            {
                streamWriter = new StreamWriter(filePath);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(CaseInformation));
                xmlSerializer.Serialize(streamWriter, caseInfo);
            }
            catch (System.IO.IOException)
            {
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }

        public static CaseInformation XmlDeserialize(string filePath)
        {
            CaseInformation caseInfo = null;
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(filePath);
                XmlSerializer deserializer = new XmlSerializer(typeof(CaseInformation));
                caseInfo = deserializer.Deserialize(reader) as CaseInformation;
            }
            catch
            {
                CaseInformation info = new CaseInformation();
                info.InitializeDefaults();
                info.InitDefaultSoundMap();
                return info;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return caseInfo;
        }

        public CaseInformation Clone()
        {
            CaseInformation clone = new CaseInformation();
            clone.caseID = this.caseID;
            clone.patientsClaim = this.patientsClaim;
            clone.diseaseName = this.diseaseName;
            clone.soundName = this.soundName;
            clone.frontViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            foreach (AuscultationSiteInfo siteInfo in this.frontViewAuscultationSites)
            {
                AuscultationSiteInfo clonedSiteInfo = siteInfo.Clone();
                clone.frontViewAuscultationSites.Add(clonedSiteInfo);
            }
            clone.backViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            foreach (AuscultationSiteInfo siteInfo in this.backViewAuscultationSites)
            {
                AuscultationSiteInfo clonedSiteInfo = siteInfo.Clone();
                clone.backViewAuscultationSites.Add(clonedSiteInfo);
            }
            clone.SoundMap = this.soundMap.Clone();
            return clone;
        }
        #endregion

        #region Private methods
        private void InitDefaultAuscultationSiteInfo()
        {
            this.frontViewAuscultationSites = new Collection<AuscultationSiteInfo>();

            //load default front view auscultation sites
            AuscultationSiteInfo frontViewAuscultationSiteInfoOne = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoOne.Location = new PointF(-17f, 29f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoTwo = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoTwo.Location = new PointF(21f, 29f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoThree = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoThree.Location = new PointF(23f, 42f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoFour = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoFour.Location = new PointF(25f, 60f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoFive = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoFive.Location = new PointF(49f, 83f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoSix = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoSix.Location = new PointF(-47f, 7f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoSeven = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoSeven.Location = new PointF(-60f, 80f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoEight = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoEight.Location = new PointF(-72f, 128f);
            AuscultationSiteInfo frontViewAuscultationSiteInfoNine = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoNine.Location = new PointF(48f, 6f); ;
            AuscultationSiteInfo frontViewAuscultationSiteInfoTen = new AuscultationSiteInfo();
			frontViewAuscultationSiteInfoTen.Location = new PointF(75f, 128f);

            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoOne);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoTwo);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoThree);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoFour);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoFive);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoSix);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoSeven);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoEight);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoNine);
            this.frontViewAuscultationSites.Add(frontViewAuscultationSiteInfoTen);


            //load back view auscultation sites
            this.backViewAuscultationSites = new Collection<AuscultationSiteInfo>();
            AuscultationSiteInfo backViewAuscultationSiteInfoOne = new AuscultationSiteInfo();
			backViewAuscultationSiteInfoOne.Location = new PointF(-36f, 11f);
            AuscultationSiteInfo backViewAuscultationSiteInfoTwo = new AuscultationSiteInfo();
			backViewAuscultationSiteInfoTwo.Location = new PointF(-51f, 82f);
            AuscultationSiteInfo backViewAuscultationSiteInfoThree = new AuscultationSiteInfo();
			backViewAuscultationSiteInfoThree.Location = new PointF(-52f, 137f);
            AuscultationSiteInfo backViewAuscultationSiteInfoFour = new AuscultationSiteInfo();
			backViewAuscultationSiteInfoFour.Location = new PointF(34f, 10f);
            AuscultationSiteInfo backViewAuscultationSiteInfoFive = new AuscultationSiteInfo();
			backViewAuscultationSiteInfoFive.Location = new PointF(73f, 132f);

            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoOne);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoTwo);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoThree);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoFour);
            this.backViewAuscultationSites.Add(backViewAuscultationSiteInfoFive);
        }

        private void InitDefaultSoundMap()
        {
            this.soundMap = new SoundMapData();
            this.soundMap.InitializeDefaults();
        }
        #endregion
    }
}
